package org.chaos.sekiro.app.web

import org.chaos.sekiro.app.internal.model.Person
import org.chaos.sekiro.app.internal.txservice.PersonTxService
import org.chaos.sekiro.common.web.RequestResult
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/test")
class TestController(
    private val personTxService: PersonTxService
) {

    @GetMapping("/hello")
    fun hello() = RequestResult(data = "hello world")

    @GetMapping("/find-person")
    fun findPerson(@RequestParam id: Long): RequestResult<Person> {
        return try {
            val person = personTxService.findById(id)
            RequestResult(data = person)
        } catch (e: Exception) {
            RequestResult(code = 500, message = "server error")
        }
    }
}